//
//  SecondViewController.m
//  内存管理综合
//
//  Created by ZhouFahong on 17/3/21.
//  Copyright © 2017年 ZhouFahong. All rights reserved.
//

#import "SecondViewController.h"
#import "Person.h"

// 协议标准宏MIN,这个宏输 两个参数并返回较小的那个
#define MIN1(A,B) ((A) <= (B)? (A) : (B))

#define MIN2(A,B) ({__typeof__(A) a = A; __typeof__(B) b = B; \a< b ? a : b })

#define __NSX_PASTE__(A,B) A##B

#define __NSMIN_IMPL__(A,B,L) ({ __typeof__(A) __NSX_PASTE__(__a,L) = (A); __typeof__(B) __NSX_PASTE__(__b,L) = (B); (__NSX_PASTE__(__a,L) < __NSX_PASTE__(__b,L)) ? __NSX_PASTE__(__a,L) : __NSX_PASTE__(__b,L); })

#define MIN3(A,B) __NSMIN_IMPL__(A,B,__COUNTER__)

// 判断系统是否是iOS7以上的版本
#define IsIOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0 ? YES : NO)

#define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL

@interface SecondViewController ()

@property(strong,nonatomic)NSTimer *timer;
@property(copy,nonatomic)NSMutableArray *arr;

@end

@implementation SecondViewController

-(void)runGCD{
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    for (int i = 0; i < 100; i++) {
        NSLog(@"%d",i+1);
        
        dispatch_async(queue, ^{
//            sleep(2);
            NSLog(@"G%d",i+1);
        });
        
        
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSMutableArray *aaa = [NSMutableArray array];
    [aaa addObject:@"ee"];
    self.arr = aaa;
//    [self.arr addObject:@"1"];
    
    
    [self.view setBackgroundColor:[UIColor yellowColor]];
    [self runGCD];
    // 自己生成的对象，自己持有
    Person *person = [Person new] ;
//    NSLog(@"retainCount = %lu",(unsigned long)[person retainCount]);
//    [person autorelease];
    [person study];
    
    [self sleepWithPerson:person];
    

//    [self guojixiangqi];
//    [person retain];
//    NSLog(@"retainCount = %lu",(unsigned long)[person retainCount]);
//    [person release];
//    NSLog(@"retainCount = %lu",(unsigned long)[person retainCount]);
//    [person release];

    // 非自己生成的对象，自己也能持有
//    id obj = [[NSMutableArray array] autorelease];;
//    NSLog(@"retainCount = %lu",(unsigned long)[obj retainCount]);
//    [obj retain];
//    NSLog(@"retainCount = %lu",(unsigned long)[obj retainCount]);
//    [obj release];
    // copy 一个对象
//    NSMutableString *string = [NSMutableString stringWithString: @"origion"];
//    NSString *stringCopy = [string copy];
//    NSMutableString *mStringCopy = [string copy];
//    NSMutableString *stringMCopy = [string mutableCopy];
//    [mStringCopy appendString:@"mm"];//error
//    [string appendString:@" origion!"];
//    [stringMCopy appendString:@"!!"];
//    Person *copyPerson = [person copy];
//    [copyPerson eat];
//    NSLog(@"retainCount = %lu",(unsigned long)[copyPerson retainCount]);
    
    NSInteger min = MIN(10, 20);
    NSLog(@"%lu",(unsigned long)min);
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(time) userInfo:nil repeats:YES];
    self.timer = timer;
    [self addButton];
    [self addImage];
    [self deepCopyOrShallowCopy];
}

// 画一个棋盘
-(void)guojixiangqi{
    
    CGFloat kwidth = 300;
    CGFloat sigleWidth = kwidth/8.0;
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 100, kwidth, kwidth)];
    contentView.backgroundColor = [UIColor whiteColor];
    
    for (int y = 0; y<8; y++) {
        CGRect rect;
        if (y % 2 == 0) { // 奇数行
            
            for (int x = 0; x < 4; x++) {
                rect = CGRectMake(sigleWidth+x*(2*sigleWidth), sigleWidth*y, sigleWidth, sigleWidth);
                UIView *view = [[UIView alloc]initWithFrame:rect];
                view.backgroundColor = [UIColor blackColor];
                [contentView addSubview:view];
            }
            
        }else{
            for (int x = 0; x < 4; x++) {
                rect = CGRectMake(x*(2*sigleWidth), sigleWidth*y, sigleWidth, sigleWidth);
                UIView *view = [[UIView alloc]initWithFrame:rect];
                view.backgroundColor = [UIColor blackColor];
                [contentView addSubview:view];
            }
        }
    }
    
    [self.view addSubview:contentView];
}

-(void)addImage{
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"stack.png"]];
    [imageView setFrame:CGRectMake(0, 80+55, KSCREEN_WIDTH, (KSCREEN_WIDTH*523.0)/750.0)];
    [self.view addSubview:imageView];
    
    
}

-(void)deepCopyOrShallowCopy{
    
    NSArray *arr = @[@[@"a", @"b"], @[@"c", @"d"]];
    NSArray *arra = [[NSArray alloc]initWithArray:arr copyItems:NO];
    NSArray *array = [arr copy];
    NSMutableArray *aaaaa = [arr mutableCopy];
    NSLog(@"arr = %p,array = %p,aaaaa = %p",&arr,&array,&aaaaa); // 打印的地址是引用对象的指针所在的地址
    
    NSString *string = @"origin";
    NSString *stringCopy = [string copy];
    NSMutableString *stringMCopy = [string mutableCopy];
    
    NSLog(@"string = %p,stringCopy = %p，stringMCopy = %p",&string,&stringCopy,&stringMCopy);// 打印的地址是引用对象的指针所在的地址
    
    
}


-(void)time{
    NSLog(@"time....");
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; // tableView
    indexPath = [NSIndexPath indexPathForItem:0 inSection:0]; // collectionView
    
//    -(<span style="color:#bb23a0">void</span>)cellImageDidLoad:(<span style="color:#733aa7">NSIndexPath</span> *)indexPath image:(<span style="color:#733aa7">UIImage</span> *)image
//    {
//        <span style="color:#733aa7">UITableViewCell</span> *cell = [<span style="color:#508187">wqTable</span> <span style="color:#401a7f">cellForRowAtIndexPath</span>:indexPath];
//        cell.<span style="color:#733aa7">imageView</span>.<span style="color:#733aa7">image</span> = image;
//    }
}
-(void)viewWillDisappear:(BOOL)animated{
    [self.timer invalidate];
}

-(void)viewWillAppear:(BOOL)animated{
    
    NSArray *arr =  self.navigationController.viewControllers;
    for (UIViewController *ctr in arr) {
        NSLog(@"%@",NSStringFromClass([ctr class]));
    }
}
-(void)addButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(55, 80, 55, 55)];
    [button setTitle:@"push" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(pushToSecondViewController) forControlEvents:UIControlEventTouchUpInside];
}
-(void)pushToSecondViewController{
    
    SecondViewController *ctr = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:ctr animated:YES];
}

-(void)sleepWithPerson:(Person*)person{
    [person sleep];
}

- (void)dealloc
{
    NSLog(@"dealloc:%@",NSStringFromClass([self class]));
//    [super dealloc];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end
